﻿using System;
using System.Xml;
using System.Collections.Generic;

using System.Text;

namespace System.Qizx
{
    public static class SystemExtensions
    {
        public static StringBuilder GetStringBuilder(this System.IO.MemoryStream stream)
        {
            stream.Position = 0;
            byte[] buffer = new byte[stream.Length];
            StringBuilder builder = new StringBuilder();
            int length = 0;

            while ((length = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                string text = Encoding.UTF8.GetString(buffer, 0, length);
                builder.Append(text);
            }
            return builder;
        }

        public static String GetString(this System.IO.MemoryStream stream)
        {
            stream.Position = 0;
            byte[] buffer = new byte[stream.Length];
            int length = 0;

            while ((length = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                string text = Encoding.UTF8.GetString(buffer, 0, length);
                return text;
            }
            return string.Empty;
        }

        public static Uri resolve(this Uri baseUri, string relative)
        {
            XmlResolver xr = new XmlUrlResolver();
            return xr.ResolveUri(baseUri, relative);
        }
        public static long DoubleToLongBits(this double value)
        {
            return BitConverter.DoubleToInt64Bits(value);
        }

        public static string replaceAll(this string source, string old, string newTExt)
        {
            return source.Replace(old, newTExt);
        }

        public static double LongBitsToDouble(this long value)
        {
            return BitConverter.Int64BitsToDouble(value);
        }
        public static string getString(this Exception ex)
        {
            return ex.Message;
        }
    }
}
